package com.example.demo.controller;

import com.example.demo.base.*;
import com.example.demo.entity.Security;
import com.example.demo.service.SecurityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Validated
@RequestMapping({"/manage/"})
public class SecurityController {


    @Autowired
    private SecurityService securityService;

    @RequestMapping(value = "/security/list")
    public BaseResponse list(
            @RequestParam(required = false, value = "name") String name,
            @RequestParam(required = false, value = "page") Integer pageNo,
            @RequestParam(required = false, value = "rows") Integer pageSize,
            HttpServletRequest request) {
        QueryBuilder queryBuilder = new QueryBuilder();
        if(StringUtils.isNotBlank(name)){
            queryBuilder.addFilter(Filter.eq("name",name));
        }
        Pageable<Security> pageable = securityService.selectEntitiesByPage(queryBuilder,pageNo,pageSize);

        BaseResponse response = new BaseResponse();
        if(pageable!=null && !CollectionUtils.isEmpty(pageable.getRecords())){
            response.setRows(pageable.getRecords());
            response.setTotal(new Long(pageable.getTotal()).intValue());
        }
        return response;
    }

    @RequestMapping(value = "/security/update")
    public BaseResponse updateUser(
            @RequestParam(required = false, value = "id") Long id,
            @RequestParam(required = false, value = "address") String address,
            @RequestParam(required = false, value = "phone") String phone,
            @RequestParam(required = false, value = "dutyInfo") String dutyInfo,
            @RequestParam(required = false, value = "rangeInfo") String rangeInfo,
            @RequestParam(required = false, value = "sex") String sex,
            @RequestParam(required = false, value = "sign") Integer sign,
            HttpServletRequest request) {
        Security security = new Security();
        if(sign==0){//删除
            security.setDeleted(1);
        }else if(sign==1){
            security.setAddress(address);
            security.setPhone(phone);
            security.setDutyInfo(dutyInfo);
            security.setRangeInfo(rangeInfo);
            security.setSex(sex);
        }else{
            security.setDeleted(0);
        }
        UpdateBuilder updateBuilder = new UpdateBuilder(security);
        updateBuilder.addFilter(Filter.eq("id",id));
        securityService.updateEntity(updateBuilder);

        return BaseResponse.success();
    }

    @RequestMapping(value = "/security/add")
    public BaseResponse add(
            @RequestParam(required = false, value = "name") String name,
            @RequestParam(required = false, value = "address") String address,
            @RequestParam(required = false, value = "phone") String phone,
            @RequestParam(required = false, value = "sex") String sex,
            HttpServletRequest request) {
        Security security = new Security();
        security.setAddress(address);
        security.setPhone(phone);
        security.setName(name);
        security.setSex(sex);

        securityService.insertEntity0(security);
        return BaseResponse.success();
    }
}
